<?php

/**
 * 首页
 */
class mySetPrice extends actionAbstract {

    public $title = '';
    public $css = array();
    public $modules = array();
    public $menu = array();
    public $pos = 4;
	public $type=0;
	public $leftpos=0;
    
	/**
	* 递归要添加的菜单PIN
	 *
	 * @var array
	 */
	protected $add_auth = array ();
	
    /**
     * 构造
     */
    function __construct() {
        parent::__construct();
          $this->info = acl::checkLogin('admininfo',$this->url('common/login'));
        //     if(!isset($_SESSION['admininfo'])){ header('location:'.$this->url('common/login'));}
		$this->menu = $this->loadConfig(); 
		$this->type = isset($_GET['type'])?(int)$_GET['type']:0;
    }
    
    //加浮管理
    public function index(){
		$this->leftpos=0;
        $this->loadModel('pms','channels');
        
        $page=isset($_GET['page'])?(int)($_GET['page']):1;
        $nameKey=isset($_GET['nameKey'])?$_GET['nameKey']:'';
        
        $channels=$this->pms->channelsModel->fetchAll("select osiiId,title from pms_channels where status=1");
		
		$sql_from=" from hotel_type as a left join hotel_category as b on b.id=a.categoryid";
		$sql_where=" where a.parentTypeId!=0";
		$sql_order_by=" order by a.categoryid,a.id asc";
        $field_reto=" count(a.id)";
		$field_re=" a.id,a.title,a.price,a.parentTypeId,b.hotelName,b.id as hotelId";
		
        if($nameKey){
            $nameKey=trim($nameKey);
            $nameKeyArr=explode(',',$nameKey);
            foreach($nameKeyArr as $k=>$v){
                $sql_where.=" and (a.title like '%".trim($v)."%' or b.hotelName like '%".trim($v)."%')";
            }
        }
		
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"hotel","type",$page,5);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];
		
		if($re){
            foreach($re as $k=>$v){
                $re[$k]['channels']=array();
                $re[$k]['channels']=$channels;
            }
        }
        
        include $this->loadWidget('masterTheme');
    }
    
    //添加 、 修改 浮动金额
    public function upSetPrice(){
        $this->loadHelper('extend');
        $this->loadModel('sys','setprice');
        $this->loadModel('hotel','price');
        $this->loadModel('pms','channels');
        $data['hotelId']=isset($_POST['hotelId'])?(int)($_POST['hotelId']):0;
        $data['parentTypeId']=isset($_POST['parentTypeId'])?(int)($_POST['parentTypeId']):0;
        $data['typeId']=isset($_POST['typeId'])?(int)($_POST['typeId']):0;
        $sta=isset($_POST['sta'])?$_POST['sta']:'';
        $end=isset($_POST['end'])?$_POST['end']:'';
        $channels=isset($_POST['channels'])?$_POST['channels']:'';
        $data['setprice']=isset($_POST['setprice'])?(int)($_POST['setprice']):0;
        $data['fudong']=isset($_POST['fudong'])?(int)($_POST['fudong']):0;
        
        if(!$sta){
            ajaxReturn ('','请选择开始时间！', 0 );exit;
        }
        if(!$end){
            ajaxReturn ('','请选择结束时间！', 0 );exit;
        }
        if($end&&(strtotime($end)-strtotime($sta))/24/3600<0){
            ajaxReturn ('','结束时间不能小于开始时间！', 0 );exit;
        }
        if(!$channels){
            ajaxReturn ('','请选择渠道！', 0 );exit;
        }
        if($data['setprice']<0){
            ajaxReturn ('','请填写正确的加浮金额！', 0 );exit;
        }
        if(!$data['fudong']){
            ajaxReturn ('','请选择浮动价格的类型！', 0 );exit;
        }
        
        $data['created']=date('Y-m-d H:i:s',time());
        $day=$end?(strtotime($end)-strtotime($sta))/24/3600:0;
        for($i=0;$i<=$day;$i++){
            $data['times']=strtotime($sta)+$i*24*3600;
            foreach($channels as $k=>$v){
                $sqla="select id from hotel_price where hotelId=".$data['hotelId']." and parentTypeId=".$data['parentTypeId'];
                $sqla.=" and typeid=".$data['typeId']." and channels='".$v."' and times=".$data['times']." and created!=''";
				$th_price=$this->hotel->priceModel->fetchRow($sqla);
                if(!$th_price){
                    $channelsName=$this->pms->channelsModel->fetchRow("select title from pms_channels where osiiId=$v");
                    ajaxReturn ('','设置失败，渠道：'.$channelsName['title'].' '.date('Y-m-d',$data['times']).' 没有房价！', 0 );exit;
                }
            }
        }
        for($i=0;$i<=$day;$i++){
            $data['times']=strtotime($sta)+$i*24*3600;
            foreach($channels as $k=>$v){
                $data['channelsId']=$v;
                $sql="select id from sys_setprice where hotelId=".$data['hotelId']." and parentTypeId=".$data['parentTypeId']." and typeId=".$data['typeId'];
                $sql.=" and times=".$data['times']." and channelsId=".$v;
                $re=$this->sys->setpriceModel->fetchRow($sql);
                if($re){
                    $this->sys->setpriceModel->update($data,"id=".$re['id']);
                }else{
                    $this->sys->setpriceModel->insert($data);
                }
                $this->setLogs("系统浮动记录：".json_encode($data));
            }
        }
        
        $this->porto($data['hotelId'],$data['parentTypeId'],$data['typeId'],strtotime($sta),strtotime($end),$channels,$data['setprice'],$data['fudong']);
        ajaxReturn ('refurbish','操作成功！', 1 );exit;
    }
	
    //改变浮动金额  -  传给OSII
    function porto($hotelId,$parentTypeId,$typeId,$sta,$end,$channels,$setprice,$fudong){
        $this->loadModel('hotel','type');
        $this->loadModel('hotel','typestatus');
        $this->loadModel('hotel','price');
        $this->loadModel('float','log');
		$this->loadModel('pms','account');
        $this->loadApi('osiiHotel');
        $osiiHotel = new osiiHotel();
        
        $priceAll=array();
        if($channels){
            foreach($channels as $k=>$v){
                $sql="select sellPrice,times from hotel_price where hotelId=$hotelId and parentTypeId=$parentTypeId";
                $sql.=" and typeid=$typeId and times>=$sta and times<=$end and channels='".$v."' and created!='' order by times asc";
                $price_th=$this->hotel->priceModel->fetchAll($sql);
                if($price_th){
                    $priceAll[$v]=$price_th;
                }
            }
        }
        
        if($priceAll){
            $ka=0;
            foreach($priceAll as $k=>$v){
                $body = array('setRoomPriceItems' => array());
                $body['setRoomPriceItems'][$ka]['priceInfos'] = array();
                $body['setRoomPriceItems'][$ka]['hotelId'] = $hotelId;
                $body['setRoomPriceItems'][$ka]['sourceId'] = $k;
                $body['setRoomPriceItems'][$ka]['roomId'] = $typeId;
                $body['setRoomPriceItems'][$ka]['startDate'] = date('Y-m-d', $sta) . 'T00:00:00';
                $body['setRoomPriceItems'][$ka]['endDate'] = date('Y-m-d', $end) . 'T00:00:00';
                $body['setRoomPriceItems'][$ka]['currency'] = "CNY";
                foreach($v as $key=>$val){
                    
                    $sellPrice=$fudong==1?$val['sellPrice']+$setprice:$val['sellPrice']-$setprice;
                    
                    $fsql="select floating,sellmoney from float_log where hotelid=$hotelId and parent_TypeId=$parentTypeId";
                    $fsql.=" and time=".$val['times'];
                    $float=$this->float->logModel->fetchRow($fsql);
                    if($float){
                        $sellPrice=$float['floating']==1?$sellPrice+$float['sellmoney']:$sellPrice-$float['sellmoney'];
                    }
                    
                    $body['setRoomPriceItems'][$ka]['priceInfos'][$key]['prices'] = array();
                    $body['setRoomPriceItems'][$ka]['priceInfos'][$key]['prices'][0]['amountBeforeTaxFee'] = $sellPrice;
                    $body['setRoomPriceItems'][$ka]['priceInfos'][$key]['prices'][0]['amountAfterTaxFee'] = $sellPrice;
                    $body['setRoomPriceItems'][$ka]['priceInfos'][$key]['prices'][0]['costAmountBeforeTaxFee'] = $sellPrice;
                    $body['setRoomPriceItems'][$ka]['priceInfos'][$key]['prices'][0]['costAmountAfterTaxFee'] = $sellPrice;
                    $body['setRoomPriceItems'][$ka]['priceInfos'][$key]['prices'][0]['day'] = 0;
                    $body['setRoomPriceItems'][$ka]['priceInfos'][$key]['stays'] = 0;
                    
                    $blanceType=$this->hotel->typestatusModel->fetchRow("select payType from hotel_typestatus where hotelId=$hotelId and typeId=$typeId and times=".$val['times']);
                    $body['setRoomPriceItems'][$ka]['priceInfos'][$key]['blanceType'] = $blanceType&&$blanceType['payType']!=''?$blanceType['payType']:"PP";
                    $body['setRoomPriceItems'][$ka]['priceInfos'][$key]['priceType'] = $blanceType&&$blanceType['payType']=='FG'?"Sell":"Cost";
                    
					//查询早餐数
					$breKpriceSql="select breakfast from hotel_price where hotelId=$hotelId and typeid=$typeId and isdetailed=0";
					$breKprice=$this->hotel->priceModel->fetchRow($breKpriceSql);
					$sort_th = $this->hotel->typeModel->fetchRow("select zisort from hotel_type where id=$typeId");
                    if ($sort_th && ($sort_th['zisort'] == 1 || $sort_th['zisort'] == 5)) {
                        $body['setRoomPriceItems'][$ka]['priceInfos'][$key]['breakfast'] = $breKprice['breakfast']?$breKprice['breakfast']:1;
                    } else {
                        $body['setRoomPriceItems'][$ka]['priceInfos'][$key]['breakfast'] = 0;
                    }
					
					//查询是否包房商设置可否取消
					$isbusinessSql="select a.isbusiness from pms_account as a left join hotel_category as b on b.partnerid=a.id where b.id=$hotelId";
					$isbusiness=$this->pms->accountModel->fetchRow($isbusinessSql);
					if($isbusiness['isbusiness']==1){
						$body['setRoomPriceItems'][$ka]['priceInfos'][$key]['isCancel'] = 0;
					}else{
						$body['setRoomPriceItems'][$ka]['priceInfos'][$key]['isCancel'] = 1;
					}
                }
                $reosii = $osiiHotel->typePrice($body);
            }
        }
    }
    
    //查询 加浮记录
    public function showLog(){
        $this->loadModel('sys','setprice');
        $this->loadModel('pms','channels');
        $hotelId=isset($_POST['hotelId'])?(int)($_POST['hotelId']):0;
        $parentTypeId=isset($_POST['parentTypeId'])?(int)($_POST['parentTypeId']):0;
        $typeId=isset($_POST['typeId'])?(int)($_POST['typeId']):0;
        $sta=isset($_POST['sta'])?$_POST['sta']:'';
        $end=isset($_POST['end'])?$_POST['end']:'';
        $channelsId=isset($_POST['channelsId'])?$_POST['channelsId']:0;
		
		$where="";
		if($sta){
			$where.=" and a.times>='".strtotime($sta)."'";
		}
		if($end){
			$where.=" and a.times<='".strtotime($end)."'";
		}
		if($channelsId){
			$where.=" and a.channelsId=$channelsId";
		}
		
        $sql="select a.*,b.title from sys_setprice as a left join pms_channels as b on b.osiiId=a.channelsId where";
        $sql.=" a.hotelId=$hotelId and a.parentTypeId=$parentTypeId and a.typeId=$typeId".$where;
		$sql.=" and a.times>='".strtotime(date('Y-m-d',time()))."' order by a.times asc";
        $re=$this->sys->setpriceModel->fetchAll($sql);
        $html="";
        if($re){
            foreach($re as $k=>$v){
                $html.="<tr>";
					$html.="<td>".date('Y-m-d',$v['times'])."</td>";
					$html.="<td style='color: red;'>".($v['fudong']==1?'+ ':'- ').$v['setprice']."</td>";
					$html.="<td>".$v['title']."</td>";
					$html.="<td>".$v['created']."</td>";
				$html.="</tr>";
            }
        }else{
			$html="<tr><td colspan='4' style='text-align: center;'>无记录</td></tr>";
		}
        exit(json_encode(array('state'=>1,'html'=>$html,'hotelId'=>$hotelId,'parentTypeId'=>$parentTypeId,'typeId'=>$typeId,'sta'=>$sta,'end'=>$end,'channelsId'=>$channelsId)));
    }
    
    //订单盈亏
    function finance(){
		$this->leftpos=1;
        $this->loadModel('pms','channels');
        $this->loadModel('sys','finance');
        global $pmsconfigs;
        
        $page=isset($_GET['page'])?(int)($_GET['page']):1;
        $nameKey=isset($_GET['nameKey'])?$_GET['nameKey']:'';
        $qudao=isset($_GET['qudao'])?$_GET['qudao']:0;
        
		$channels=$this->pms->channelsModel->fetchAll("select osiiId,title from pms_channels where status=1");
		
		$sql_from=" from sys_finance as a left join hotel_category as b on b.id=a.hotelId left join hotel_type as c";
		$sql_from.=" on c.id=a.typeId left join pms_orders as d on a.orderId=d.orderId and a.hotelId=d.hotelId and";
		$sql_from.=" a.typeId=d.roomId left join pms_channels as e on a.sourceId=e.osiiId";
		$sql_where=" where (d.status=1 or d.status=3 or d.status=4)";
		$sql_order_by=" order by d.created desc";
		$field_reto=" count(a.id)";
		$field_re=" a.orderId,a.price,a.getPrice,a.floatPrice,a.hotelPrice,a.payType,b.hotelName,c.title,";
		$field_re.="d.startDate,d.endDate,d.price as orderPrice,d.countRoom,d.created,e.title as sourceName";
		
        if($nameKey){
            $nameKeyArr=explode(',',$nameKey);
            foreach($nameKeyArr as $k=>$v){
                $sql_where.=" and (a.orderId like '%".trim($v)."%' or b.hotelName like '%".trim($v)."%' or c.title like '%".trim($v)."%')";
            }
        }
        if($qudao){
            $sql_where.=" and a.sourceId='".$qudao."'";
        }
		
		$sql="select".$field_reto." as thcount,sum(a.price) as allprices".$sql_from.$sql_where.$sql_order_by;
        $reto=$this->sys->financeModel->fetchRow($sql);
        $all_prices=$reto['allprices'];
		
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"sys","finance",$page);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];
		
        $one_prices=0;
		if($re){
			foreach($re as $k=>$v){
				$one_prices=($one_prices)+($v['price']);
			}
		}
        
        include $this->loadWidget('masterTheme');
    }
    
    //导出订单盈亏
    function export(){
        $this->loadModel('hotel','category');
        $this->loadModel('hotel','type');
        $this->loadModel('pms','channels');
        $this->loadModel('pms','orders');
        $this->loadModel('sys','finance');
        global $pmsconfigs;
        
        $nameKey=isset($_GET['nameKey'])?$_GET['nameKey']:'';
        $qudao=isset($_GET['qudao'])?$_GET['qudao']:0;
        
        $where="";
        if($nameKey){
            $nameKeyArr=explode(',',$nameKey);
            foreach($nameKeyArr as $k=>$v){
                $where.=" and (a.orderId like '%".trim($v)."%' or b.hotelName like '%".trim($v)."%' or c.title like '%".trim($v)."%')";
            }
        }
        if($qudao){
            $where.=" and a.sourceId='".$qudao."'";
        }
        $sql="select a.orderId,a.price,a.getPrice,a.floatPrice,a.hotelPrice,a.payType,b.hotelName,c.title,d.startDate,d.endDate,";
		$sql.="d.price as orderPrice,d.countRoom,d.created,e.title as sourceName";
        $sql.=" from sys_finance as a left join hotel_category as b on b.id=a.hotelId left join hotel_type as c on c.id=a.typeId";
        $sql.=" left join pms_orders as d on a.orderId=d.orderId and a.hotelId=d.hotelId and a.typeId=d.roomId";
        $sql.=" left join pms_channels as e on a.sourceId=e.osiiId where (d.status=1 or d.status=3 or d.status=4)".$where." order by d.created desc";
        
        $re=$this->sys->financeModel->fetchAll($sql);
        
        $title="订单盈亏报表".date("Y-m-d_H-i-s",time());
        $filed=array('序号','订单号','所属酒店','所属房型','入住时间','离店时间','间/夜','盈亏金额(元)','OSII传入金额(元)','酒店浮动金额(元)','订单金额(元)','酒店价格(元)','渠道来源','支付类型','订单生成时间');
        $result = array();
		foreach($re as $k=>$val){
            $result[$k]['id']	      =	$k+1;
            $result[$k]['orderId']	  =	$val['orderId'];
            $result[$k]['hotelName']  =	$val['hotelName'];
            $result[$k]['title']	  =	$val['title'];
            $result[$k]['startDate']  =	date('Y-m-d',$val['startDate']);
            $result[$k]['endDate']	  =	date('Y-m-d',$val['endDate']);
            $result[$k]['jianYe']     =	$val['countRoom'].'/'.($val['endDate']-$val['startDate'])/3600/24;
            $result[$k]['price']      =	$val['price'];
            $result[$k]['getPrice']	  =	$val['getPrice'];
            $result[$k]['floatPrice'] =	$val['floatPrice'];
            $result[$k]['orderPrice'] =	$val['orderPrice'];
            $result[$k]['hotelPrice'] =	$val['hotelPrice'];
            $result[$k]['sourceName'] =	$val['sourceName'];
            $result[$k]['payType'] =	$pmsconfigs['payType'][$val['payType']];
            $result[$k]['created']    =	date('Y-m-d H:i:s',$val['created']);
		}
        $this->outExcel($title,$filed,$result);
    }
    
	//系统推送房价、房态页面
	function hotelPrice(){
		$this->leftpos=2;
		
        $nameKey=isset($_GET['nameKey'])?$_GET['nameKey']:'';
		$page=isset($_GET['page'])?(int)($_GET['page']):1;
        
		$sql_from=" from hotel_type as a left join hotel_category as b on a.categoryid=b.id";
		$sql_where=" where a.parentTypeId!=0";
		$sql_order_by=" order by a.categoryid,a.id asc";
		$field_reto=" count(a.id)";
		$field_re=" a.categoryid,a.parentTypeId,a.id,a.price,a.title,b.hotelName";
		
        if($nameKey){
            $nameKeyArr=explode(',',$nameKey);
            foreach($nameKeyArr as $k=>$v){
                $sql_where.=" and (b.hotelName like '%".trim($v)."%' or a.title like '%".trim($v)."%')";
            }
        }
		
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"hotel","type",$page);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];$size=$return['size'];
		
		include $this->loadWidget('masterTheme');
	}
	//系统推送房价--操作
	function hotelPriceTO(){
        $this->loadModel('hotel','price');
        $this->loadModel('hotel','type');
        $this->loadModel('hotel','typestatus');
        $this->loadModel('float','log');
        $this->loadModel('sys','setprice');
		$this->loadModel('pms','account');
        $this->loadApi('osiiHotel');
        $osiiHotel = new osiiHotel();
		
        $hotelId=isset($_POST['hotelId'])?(int)($_POST['hotelId']):0;
        $parentTypeId=isset($_POST['parentTypeId'])?(int)($_POST['parentTypeId']):0;
        $typeid=isset($_POST['typeid'])?(int)($_POST['typeid']):0;
        
        $where="";
        //单一酒店房型推送
        if($hotelId&&$typeid&&$parentTypeId){
            $where=" and hotelId=$hotelId and parentTypeId=$parentTypeId and typeid=$typeid";
        }
        
        $sql="select typeid,parentTypeId,hotelId,times,sellPrice,channels from hotel_price where isdetailed=1 and sellPrice!=0";
        $sql.=" and LOCATE(',',channels)<1 and times>=".strtotime(date('Y-m-d',time())).$where;
        $re=$this->hotel->priceModel->fetchAll($sql);
        
        if($re){
            foreach($re as $k=>$v){
                $floatSql="select sellmoney,floating from float_log where hotelid=".$v['hotelId']." and parent_TypeId=".$v['parentTypeId']." and time=".$v['times'];
                $floatPrice=$this->float->logModel->fetchRow($floatSql);
                if($floatPrice&&$floatPrice['floating']==1){
                    $re[$k]['sellPrice']=$re[$k]['sellPrice']+$floatPrice['sellmoney'];
                }elseif($floatPrice&&$floatPrice['floating']==2){
                    $re[$k]['sellPrice']=$re[$k]['sellPrice']-$floatPrice['sellmoney'];
                }
                
                $setSql="select setprice,fudong from sys_setprice where hotelId=".$v['hotelId']." and parentTypeId=".$v['parentTypeId'];
                $setSql.=" and typeId=".$v['typeid']." and times=".$v['times']." and channelsId=".$v['channels'];
                $sysSetprice=$this->sys->setpriceModel->fetchRow($setSql);
                if($sysSetprice && $sysSetprice['fudong']==1){
                    $re[$k]['sellPrice']=$re[$k]['sellPrice']+$sysSetprice['setprice'];
                }elseif($sysSetprice && $sysSetprice['fudong']==2){
                    $re[$k]['sellPrice']=$re[$k]['sellPrice']-$sysSetprice['setprice'];
                }
            }
            
            $ka=0;
            foreach($re as $k=>$v){
                $body = array('setRoomPriceItems' => array());
                $body['setRoomPriceItems'][$ka]['priceInfos'] = array();
                $body['setRoomPriceItems'][$ka]['hotelId'] = $v['hotelId'];
                $body['setRoomPriceItems'][$ka]['sourceId'] = $v['channels'];
                $body['setRoomPriceItems'][$ka]['roomId'] = $v['typeid'];
                $body['setRoomPriceItems'][$ka]['startDate'] = date('Y-m-d', $v['times']) . 'T00:00:00';
                $body['setRoomPriceItems'][$ka]['endDate'] = date('Y-m-d', $v['times']) . 'T00:00:00';
                $body['setRoomPriceItems'][$ka]['currency'] = "CNY";
                $body['setRoomPriceItems'][$ka]['priceInfos'][$ka]['prices'] = array();
                $body['setRoomPriceItems'][$ka]['priceInfos'][$ka]['prices'][0]['amountBeforeTaxFee'] = $v['sellPrice'];
                $body['setRoomPriceItems'][$ka]['priceInfos'][$ka]['prices'][0]['amountAfterTaxFee'] = $v['sellPrice'];
                $body['setRoomPriceItems'][$ka]['priceInfos'][$ka]['prices'][0]['costAmountBeforeTaxFee'] = $v['sellPrice'];
                $body['setRoomPriceItems'][$ka]['priceInfos'][$ka]['prices'][0]['costAmountAfterTaxFee'] = $v['sellPrice'];
                $body['setRoomPriceItems'][$ka]['priceInfos'][$ka]['prices'][0]['day'] = 0;
                $body['setRoomPriceItems'][$ka]['priceInfos'][$ka]['stays'] = 0;
                
                $blanceType=$this->hotel->typestatusModel->fetchRow("select payType from hotel_typestatus where hotelId=".$v['hotelId']." and typeId=".$v['typeid']." and times=".$v['times']);
                $body['setRoomPriceItems'][$ka]['priceInfos'][$ka]['blanceType'] = $blanceType&&$blanceType['payType']!=''?$blanceType['payType']:"PP";
                $body['setRoomPriceItems'][$ka]['priceInfos'][$ka]['priceType'] = $blanceType&&$blanceType['payType']=='FG'?"Sell":"Cost";
                
				//查询早餐数
				$breKpriceSql="select breakfast from hotel_price where hotelId=".$v['hotelId']." and typeid=".$v['typeid']." and isdetailed=0";
				$breKprice=$this->hotel->priceModel->fetchRow($breKpriceSql);
				$sort_th = $this->hotel->typeModel->fetchRow("select zisort from hotel_type where id=".$v['typeid']);
                if ($sort_th && ($sort_th['zisort'] == 1 || $sort_th['zisort'] == 5)) {
                    $body['setRoomPriceItems'][$ka]['priceInfos'][$ka]['breakfast'] = $breKprice['breakfast']?$breKprice['breakfast']:1;
                } else {
                    $body['setRoomPriceItems'][$ka]['priceInfos'][$ka]['breakfast'] = 0;
                }
				
				//查询是否包房商设置可否取消
				$isbusinessSql="select a.isbusiness from pms_account as a left join hotel_category as b on b.partnerid=a.id where b.id=".$v['hotelId'];
				$isbusiness=$this->pms->accountModel->fetchRow($isbusinessSql);
				if($isbusiness['isbusiness']==1){
					$body['setRoomPriceItems'][$ka]['priceInfos'][$ka]['isCancel'] = 0;
				}else{
					$body['setRoomPriceItems'][$ka]['priceInfos'][$ka]['isCancel'] = 1;
				}
				
                $reosii = $osiiHotel->typePrice($body);
            }
            exit(json_encode(array('state' => 1, 'html' => '操作成功！')));
        }
        exit(json_encode(array('state' => 0, 'html' => '无价格推送！')));
	}
	
    //酒店房态推送
    function hotelStatusTO(){
        $this->loadModel('hotel','category');
        $this->loadModel('hotel','type');
        $this->loadModel('hotel','typestatus');
        $this->loadModel('hotel','countrooms');
        $this->loadModel('pms','account');
        $this->loadApi('osiiHotel');
        $osiiHotel = new osiiHotel();
		
        $hotelId=isset($_POST['hotelId'])?(int)($_POST['hotelId']):0;
        $parentTypeId=isset($_POST['parentTypeId'])?(int)($_POST['parentTypeId']):0;
        $typeId=isset($_POST['typeid'])?(int)($_POST['typeid']):0;
		
		$where="";
		if($hotelId&&$typeId){
			$where=" and a.hotelId=$hotelId and a.typeId=$typeId";
		}
		$sql="select a.hotelId,a.typeId,a.status,a.times,a.lateReserveTime,a.payType,a.lateCancelTime,a.guarantee";
		$sql.=",c.roomNum,d.alreadyRoom,f.isbusiness";
		$sql.=" from hotel_typestatus as a left join hotel_type as b on b.categoryid=a.hotelId and b.id=a.typeId";
		$sql.=" left join hotel_type as c on c.id=b.parentTypeId left join hotel_countrooms as d on d.hotelId=a.hotelId";
		$sql.=" and d.typeId=b.parentTypeId and d.created=a.times left join hotel_category as e on e.id=a.hotelId";
		$sql.=" left join pms_account as f on f.id=e.partnerid where b.parentTypeId!=''";
		$sql.=" and a.times>=".strtotime(date('Y-m-d',time())).$where;
		$re=$this->hotel->typestatusModel->fetchAll($sql);
		if($re){
			foreach($re as $k=>$v){
				$body=array();
				$body['hotelCode']=$v['hotelId'];
				$body['startDate']=date('Y-m-d',$v['times']);
				$body['endDate']=date('Y-m-d',$v['times']);
				$body['editer']='uni1001';
				$body['roomID']=$v['typeId'];
				$body['roomInfoList']=array();
				$fl=0;
				if($v['status']!=0){
					$fl=empty($v['alreadyRoom'])?$v['roomNum']:($v['roomNum']-$v['alreadyRoom']>0?$v['roomNum']-$v['alreadyRoom']:0);
				}
				if($v['guarantee']){
					$userLimited_allNeedGuarantee=explode('-',$v['guarantee']);
				}
				$body['roomInfoList'][0]['fl']=$fl;
				$body['roomInfoList'][0]['setToDate']=date('Y-m-d',$v['times']);
				$body['roomInfoList'][0]['changeDefault']='T';
				$body['roomInfoList'][0]['holdDeadline']='9999';
				$body['roomInfoList'][0]['note']='';
				$body['roomInfoList'][0]['reserveTime']='0';
				$body['roomInfoList'][0]['roomstatus']=$fl==0?'N':'G';
				$body['roomInfoList'][0]['roomsInv']='0';
				$body['roomInfoList'][0]['guarantee']='';
				$body['roomInfoList'][0]['guaranteeLCT']=$v['lateCancelTime']?$v['lateCancelTime']:0;
				$body['roomInfoList'][0]['prepayLCT']=$v['isbusiness']==1?23988:($v['lateCancelTime']?$v['lateCancelTime']:0);
				$body['roomInfoList'][0]['allNeedGuarantee']=$v['payType']=='FG'?$userLimited_allNeedGuarantee[1]:'F';
				$body['roomInfoList'][0]['lateReserveTime']=empty($v['lateReserveTime'])?0:$v['lateReserveTime'];
				$body['roomInfoList'][0]['deductType']='C';
				$body['roomInfoList'][0]['blanceType']=empty($v['payType'])?'PP':$v['payType'];
				$body['roomInfoList'][0]['restorable']='T';
				$body['roomInfoList'][0]['checkType']='C';
				$body['roomInfoList'][0]['userLimited']=$v['payType']=='FG'?$userLimited_allNeedGuarantee[0]:'2';
				$reosii=$osiiHotel->roomstatus($body);
			}
			exit(json_encode(array('state' => 1, 'html' => '操作成功！')));
		}
        exit(json_encode(array('state' => 0, 'html' => '无房态推送！')));
    }
    
    //订单列表
    function orders(){
		$this->leftpos=3;
        $this->loadModel('pms','channels');
        global $pmsconfigs;
        $channels=$this->pms->channelsModel->fetchAll("select osiiId,title from pms_channels where status=1");
        
        $nameKey=isset($_GET['nameKey'])?$_GET['nameKey']:'';
        $sta=isset($_GET['sta'])?$_GET['sta']:'';
        $end=isset($_GET['end'])?$_GET['end']:'';
        $qudao=isset($_GET['qudao'])?(int)($_GET['qudao']):0;
        $payType=isset($_GET['payType'])?$_GET['payType']:0;
		$page=isset($_GET['page'])?(int)($_GET['page']):1;
        
		$sql_from=" from pms_orders as a left join hotel_category as b on b.id=a.hotelId left join hotel_type as c on c.id=a.roomId";
		$sql_from.=" left join pms_channels as d on d.osiiid=a.type left join sys_finance as e on e.orderId=a.orderId";
		$sql_from.=" and e.hotelId=a.hotelId and e.typeId=a.roomId and e.sourceId=a.type";
		$sql_where=" where a.id>0";
		$sql_order_by=" order by a.created desc";
		$field_reto=" count(a.id)";
		$field_re=" a.orderId,a.startDate,a.endDate,a.countRoom,a.price,a.status,a.created,a.payType,";
		$field_re.="a.name,b.hotelName,c.title,d.title as sourceName,e.floatPrice,e.hotelPrice";
		
        if($nameKey){
            $nameKeyArr=explode(',',$nameKey);
            foreach($nameKeyArr as $k=>$v){
                $sql_where.=" and (a.orderId like '%".trim($v)."%' or b.hotelName like '%".trim($v)."%' or c.title like '%".trim($v)."%')";
            }
        }
        if($qudao){
            $sql_where.=" and d.osiiid=$qudao";
        }
        if($payType){
            $sql_where.=" and a.payType='".$payType."'";
        }
        if($sta){
            $sql_where.=" and a.endDate>=".strtotime($sta);
        }
        if($end){
            $sql_where.=" and a.endDate<=".strtotime($end);
        }
        
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"pms","orders",$page);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];
		
        include $this->loadWidget('masterTheme');
    }
    
    //导出订单
    function exportOrder(){
        $this->loadModel('pms','orders');
        $this->loadModel('pms','channels');
        $this->loadModel('hotel','category');
        $this->loadModel('hotel','type');
        global $pmsconfigs;
        
        $nameKey=isset($_GET['nameKey'])?$_GET['nameKey']:'';
        $sta=isset($_GET['sta'])?$_GET['sta']:'';
        $end=isset($_GET['end'])?$_GET['end']:'';
        $qudao=isset($_GET['qudao'])?(int)($_GET['qudao']):0;
        $payType=isset($_GET['payType'])?$_GET['payType']:0;
		$page=isset($_GET['page'])?(int)($_GET['page']):1;
        
        $where="";
        if($nameKey){
            $nameKeyArr=explode(',',$nameKey);
            foreach($nameKeyArr as $k=>$v){
                $where.=" and (a.orderId like '%".trim($v)."%' or b.hotelName like '%".trim($v)."%' or c.title like '%".trim($v)."%')";
            }
        }
        if($qudao){
            $where.=" and d.osiiid=$qudao";
        }
        if($payType){
            $where.=" and a.payType='".$payType."'";
        }
        if($sta){
            $where.=" and a.endDate>=".strtotime($sta);
        }
        if($end){
            $where.=" and a.endDate<=".strtotime($end);
        }
        
        $sql="select a.orderId,a.startDate,a.endDate,a.countRoom,a.price,a.status,a.created,a.payType,";
        $sql.="a.name,b.hotelName,c.title,d.title as sourceName,e.floatPrice,e.hotelPrice";
        $sql.=" from pms_orders as a left join hotel_category as b on b.id=a.hotelId left join hotel_type as c on c.id=a.roomId";
        $sql.=" left join pms_channels as d on d.osiiid=a.type left join sys_finance as e on e.orderId=a.orderId";
        $sql.=" and e.hotelId=a.hotelId and e.typeId=a.roomId and e.sourceId=a.type where (a.status=1 or a.status=3 or a.status=4)".$where;
        $sql.=" order by b.id,d.osiiid,a.status,a.created asc";
        $re=$this->pms->ordersModel->fetchAll($sql);
        
        $title="订单表".date("Y-m-d_H-i-s",time());
        $filed=array('序号','订单号','酒店名称','房型名称','预订间数','预订天数','预订来源','订单客户','入住时间','离店时间','结算总价(元)','支付类型','订单状态','下单时间');
        $result = array();
		foreach($re as $k=>$val){
            $result[$k]['id']	      =	$k+1;
            $result[$k]['orderId']	  =	$val['orderId'];
            $result[$k]['hotelName']  =	$val['hotelName'];
            $result[$k]['title']	  =	$val['title'];
            $result[$k]['jianshu']    =	$val['countRoom'];
            $result[$k]['tianshu']    =	($val['endDate']-$val['startDate'])/3600/24;
            $result[$k]['sourceName'] =	$val['sourceName'];
            $result[$k]['name']       =	$val['name'];
            $result[$k]['startDate']  =	date('Y-m-d',$val['startDate']);
            $result[$k]['endDate']	  =	date('Y-m-d',$val['endDate']);
            $result[$k]['price']      =	$val['payType']=='PP'?$val['price']:$val['price']-$val['floatPrice']-$val['hotelPrice'];
            $result[$k]['payType']    =	$pmsconfigs['payType'][$val['payType']];
            $result[$k]['status']     =	$pmsconfigs['orderstatus'][$val['status']];
            $result[$k]['created']    =	date('Y-m-d H:i:s',$val['created']);
		}
        $this->outExcel($title,$filed,$result);
    }
}